<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;


class GroupCat extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%group_cat}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'describe'], 'trim'],
            [['title', 'describe'], 'required','message' => '{attribute}不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'title' => '标题',
            'describe' => '内容',
            'set_posts_id' => '发帖人id',
            'set_posts_name' => '发帖人',
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'title',
                'describe',
                'sort',
                'banner',
                'video',
            ],
            'create' => [
                'id',
                'title',
                'describe',
                'img',
                'set_posts_id',
                'set_posts_name'.
                'status',
                'create_time',
                'update_time',
            ],
            'update' => [
                '!id',
                'title',
                'describe',
                'img',
                'sort',
                'banner',
                'video',
                'admin_id',
                'admin_name'.
                'status',
                'create_time',
                'update_time',
                'deleted_at',
            ],
        ];
    }

    /**
     * 更新数据
     * @author hey_heyang(635870453@qq.com)
     */
    public static function saveInfo($scenario, $data, $id = 0){
        if (!in_array($scenario, ['create', 'update'])) {
            return false;
        }
        $t = static::getDb()->beginTransaction();
        try {
            if ($scenario == 'update') {
                $id = intval($id);
                if (!$id) {
                    return false;
                }
                $model           = static::findOne(['id' =>$id]);
                if(empty($model)) throw new Exception('不存在此用户');
                $model->scenario = 'update';
            } else {
                $model           = new self();
                $model->scenario = 'create';
            }
            $model->setAttributes($data, false);
            if (!$model->save()) {
                $t->rollBack();
                return $model;
            }
            $model->refresh();
            $t->commit();
            return $model->attributes['id'];
            //return $model;
        } catch (Exception $e) {
            $t->rollback();
            return $e;
        }
    }

    /**
     * @inheritdoc
     */
    public static function selectFieldData($data = [],$field = '*',$member = [])
    {
        $model = new \frontend\models\Group();
        $query = new \yii\db\Query();
        $query->select($field)->from('group_cat');
        $query->where(['deleted_at' => null]);
        $query->andWhere(['status' => 1]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);

        $command = $query->createCommand();
        $result = $command->queryAll();

        foreach ($result as $k => $v){
            $result[$k]['group'] = $model::selectLimitInfo(['group_cat_id' => $v['id']],'`id`,`name`,`cover`',0,3);
        }
        return $result;
    }




}
